package com.binance.notification.web.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.binance.master.utils.CopyBeanUtils;
import com.binance.master.utils.DateUtils;
import com.binance.notification.api.request.AddAbnormalConfigRequest;
import com.binance.notification.api.request.GetAbnormalConfigRequest;
import com.binance.notification.api.request.UpdateAbnormalConfigRequest;
import com.binance.notification.api.response.GetAbnormalConfigResponse;
import com.binance.notification.api.vo.PagingResult;
import com.binance.notification.data.mapper.AbnormalThresholdConfigMapper;
import com.binance.notification.data.models.AbnormalThresholdConfigModel;
import com.binance.notification.web.service.IAbnormalThresholdConfigService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

@Service
public class AbnormalThresholdConfigServiceImpl implements IAbnormalThresholdConfigService {

    @Autowired
    private AbnormalThresholdConfigMapper abnormalThresholdConfigMapper;

    @Override
    public PagingResult<GetAbnormalConfigResponse> getAbnormalConfigList(GetAbnormalConfigRequest body)
            throws Exception {
        Map<String, Object> param = Maps.newHashMap();
        param.put("symbol", body.getSymbol());
        param.put("businessType", body.getBusinessType());
        param.put("configStartTime", body.getStartDate());
        param.put("configEndTime", body.getEndDate());
        List<AbnormalThresholdConfigModel> list = this.abnormalThresholdConfigMapper.getAbnormalThresholdConfig(param);
        if (list == null) {
            return new PagingResult<>();
        }
        List<GetAbnormalConfigResponse> result = Lists.transform(list, item -> {
            return CopyBeanUtils.copy(item, GetAbnormalConfigResponse.class);
        });
        return new PagingResult<>(result, this.abnormalThresholdConfigMapper.getAbnormalThresholdConfigCount(param));
    }

    @Override
    public int addAbnormalConfig(AddAbnormalConfigRequest body) throws Exception {
        return abnormalThresholdConfigMapper.insert(CopyBeanUtils.copy(body, AbnormalThresholdConfigModel.class));
    }

    @Override
    public int updateAbnormalConfig(UpdateAbnormalConfigRequest body) throws Exception {
        AbnormalThresholdConfigModel model = CopyBeanUtils.copy(body, AbnormalThresholdConfigModel.class);
        model.setUpdateTime(DateUtils.getNewUTCDate());
        return this.abnormalThresholdConfigMapper.updateByPrimaryKeySelective(model);
    }

    @Override
    public int deleteAbnormalConfig(List<Long> id) throws Exception {
        return this.abnormalThresholdConfigMapper.deleteByPrimaryKey(id);
    }

    // @Override
    // public AbnormalThresholdConfigModel findBySymbolAndBusinessType(String symbol, String
    // businessType)
    // throws Exception {
    // Map<String, Object> param = Maps.newHashMap();
    // param.put("symbol", symbol);
    // param.put("businessType", businessType);
    // return abnormalThresholdConfigMapper.findBySymbolAndBusinessType(param);
    // }
    //
    //
    @Override
    public GetAbnormalConfigResponse findById(Long id) throws Exception {
        AbnormalThresholdConfigModel model = this.abnormalThresholdConfigMapper.selectByPrimaryKey(id);
        if (model == null) {
            return null;
        }
        return CopyBeanUtils.copy(model, GetAbnormalConfigResponse.class);
    }



}
